﻿/********************************************************************************
** 作者： Mick
** 邮箱： zheng_jinfan@126.com
** 主页： http://www.zhengjinfan.cn
** 创始时间：2016-2-17
** 描述：
**      [辅助类]Html辅助类
*********************************************************************************/

using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;

namespace BestEasyCMS.Framework.Util
{
    public class HtmlUtil
    {
        #region 构造实例

        /// <summary>
        /// 构建类的实例对象
        /// </summary>
        public static HtmlUtil Instance
        {
            get { return new HtmlUtil(); }
        }
        #endregion 构造实例

        #region 私有字段

        private static readonly CookieContainer Cc = new CookieContainer();
        private const string ContentType = "application/x-www-form-urlencoded";

        private const string Accept =
            "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight-2-b1, */*";

        private const string UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";

        private static Encoding _encoding = Encoding.GetEncoding("utf-8");
        private static int _delay = 1000;
        private static int _maxTry = 300;
        private static int _currentTry;

        #endregion

        #region 公有属性

        /// <summary> 
        /// Cookie
        /// </summary> 
        public static CookieContainer CookieContainer
        {
            get { return Cc; }
        }

        /// <summary> 
        /// 语言
        /// </summary> 
        public static Encoding Encoding
        {
            get { return _encoding; }
            set { _encoding = value; }
        }

        public static int NetworkDelay
        {
            get
            {
                var r = new Random();
                return (r.Next(_delay, _delay*2));
            }
            set { _delay = value; }
        }

        public static int MaxTry
        {
            get { return _maxTry; }
            set { _maxTry = value; }
        }

        #endregion

        #region 获取HTML

        /// <summary>
        /// 获取HTML
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="postData">post 提交的字符串</param>
        /// <param name="isPost">是否是post</param>
        /// <param name="cookieContainer">CookieContainer</param>
        public static string GetHtml(string url, string postData, bool isPost, CookieContainer cookieContainer)
        {
            if (string.IsNullOrEmpty(postData)) return GetHtml(url, cookieContainer);
            Thread.Sleep(NetworkDelay);
            _currentTry++;
            HttpWebRequest httpWebRequest = null;
            HttpWebResponse httpWebResponse = null;
            try
            {
                var byteRequest = Encoding.Default.GetBytes(postData);
                httpWebRequest = (HttpWebRequest) WebRequest.Create(url);
                httpWebRequest.CookieContainer = cookieContainer;
                httpWebRequest.ContentType = ContentType;
                httpWebRequest.ServicePoint.ConnectionLimit = _maxTry;
                httpWebRequest.Referer = url;
                httpWebRequest.Accept = Accept;
                httpWebRequest.UserAgent = UserAgent;
                httpWebRequest.Method = isPost ? "POST" : "GET";
                httpWebRequest.ContentLength = byteRequest.Length;
                var stream = httpWebRequest.GetRequestStream();
                stream.Write(byteRequest, 0, byteRequest.Length);
                stream.Close();
                httpWebResponse = (HttpWebResponse) httpWebRequest.GetResponse();
                var responseStream = httpWebResponse.GetResponseStream();
                if (responseStream != null)
                {
                    var streamReader = new StreamReader(responseStream, _encoding);
                    var html = streamReader.ReadToEnd();
                    streamReader.Close();
                    _currentTry = 0;
                    httpWebRequest.Abort();
                    httpWebResponse.Close();
                    return html;
                }
                return string.Empty;
            }
            catch
            {
                if (_currentTry <= _maxTry) GetHtml(url, postData, isPost, cookieContainer);
                _currentTry--;
                if (httpWebRequest != null) httpWebRequest.Abort();
                if (httpWebResponse != null) httpWebResponse.Close();
                return string.Empty;
            }
        }

        /// <summary>
        /// 获取HTML
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="cookieContainer">CookieContainer</param>
        public static string GetHtml(string url, CookieContainer cookieContainer)
        {
            Thread.Sleep(NetworkDelay);
            _currentTry++;
            HttpWebRequest httpWebRequest = null;
            HttpWebResponse httpWebResponse = null;
            try
            {
                httpWebRequest = (HttpWebRequest) WebRequest.Create(url);
                httpWebRequest.CookieContainer = cookieContainer;
                httpWebRequest.ContentType = ContentType;
                httpWebRequest.ServicePoint.ConnectionLimit = _maxTry;
                httpWebRequest.Referer = url;
                httpWebRequest.Accept = Accept;
                httpWebRequest.UserAgent = UserAgent;
                httpWebRequest.Method = "GET";
                httpWebResponse = (HttpWebResponse) httpWebRequest.GetResponse();
                var responseStream = httpWebResponse.GetResponseStream();
                if (responseStream != null)
                {
                    var streamReader = new StreamReader(responseStream, _encoding);
                    var html = streamReader.ReadToEnd();
                    streamReader.Close();
                    _currentTry--;
                    httpWebRequest.Abort();
                    httpWebResponse.Close();
                    return html;
                }
                return string.Empty;
            }
            catch
            {
                if (_currentTry <= _maxTry) GetHtml(url, cookieContainer);
                _currentTry--;
                if (httpWebRequest != null) httpWebRequest.Abort();
                if (httpWebResponse != null) httpWebResponse.Close();
                return string.Empty;
            }
        }

        #endregion

        #region 获取字符流

        /// <summary>
        /// 获取字符流
        /// </summary>
        //---------------------------------------------------------------------------------------------------------------
        // 示例:
        // System.Net.CookieContainer cookie = new System.Net.CookieContainer(); 
        // Stream s = HttpHelper.GetStream("http://ptlogin2.qq.com/getimage?aid=15000102&0.43878429697395826", cookie);
        // picVerify.Image = Image.FromStream(s);
        //---------------------------------------------------------------------------------------------------------------
        /// <param name="url">地址</param>
        /// <param name="cookieContainer">cookieContainer</param>
        public static Stream GetStream(string url, CookieContainer cookieContainer)
        {
            _currentTry++;

            HttpWebRequest httpWebRequest = null;
            HttpWebResponse httpWebResponse = null;

            try
            {
                httpWebRequest = (HttpWebRequest) WebRequest.Create(url);
                httpWebRequest.CookieContainer = cookieContainer;
                httpWebRequest.ContentType = ContentType;
                httpWebRequest.ServicePoint.ConnectionLimit = _maxTry;
                httpWebRequest.Referer = url;
                httpWebRequest.Accept = Accept;
                httpWebRequest.UserAgent = UserAgent;
                httpWebRequest.Method = "GET";

                httpWebResponse = (HttpWebResponse) httpWebRequest.GetResponse();
                var responseStream = httpWebResponse.GetResponseStream();
                _currentTry--;
                return responseStream;
            }
            catch
            {
                if (_currentTry <= _maxTry)
                {
                    GetHtml(url, cookieContainer);
                }

                _currentTry--;

                if (httpWebRequest != null)
                {
                    httpWebRequest.Abort();
                }
                if (httpWebResponse != null)
                {
                    httpWebResponse.Close();
                }
                return null;
            }
        }

        #endregion

        #region 清除HTML标记

        /// <summary>   
        /// 清除HTML标记   
        /// </summary>   
        /// <param name="htmlString">HTML</param>
        /// <returns>已经去除后的文字</returns>   
        public static string NoHtml(string htmlString)
        {
            //删除脚本   
            htmlString = Regex.Replace(htmlString, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);

            //删除HTML   
            var regex = new Regex("<.+?>", RegexOptions.IgnoreCase);
            htmlString = regex.Replace(htmlString, "");
            htmlString = Regex.Replace(htmlString, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
            htmlString = Regex.Replace(htmlString, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
            htmlString = Regex.Replace(htmlString, @"-->", "", RegexOptions.IgnoreCase);
            htmlString = Regex.Replace(htmlString, @"<!--.*", "", RegexOptions.IgnoreCase);

            htmlString = Regex.Replace(htmlString, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
            htmlString = Regex.Replace(htmlString, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
            htmlString = Regex.Replace(htmlString, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
            htmlString = Regex.Replace(htmlString, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
            htmlString = Regex.Replace(htmlString, @"&(nbsp|#160);", "   ", RegexOptions.IgnoreCase);
            htmlString = Regex.Replace(htmlString, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
            htmlString = Regex.Replace(htmlString, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
            htmlString = Regex.Replace(htmlString, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
            htmlString = Regex.Replace(htmlString, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
            htmlString = Regex.Replace(htmlString, @"&#(\d+);", "", RegexOptions.IgnoreCase);

            htmlString = htmlString.Replace("<", "");
            htmlString = htmlString.Replace(">", "");
            htmlString = htmlString.Replace("\r\n", "");

            return htmlString;
        }

        #endregion

        #region 匹配页面的链接

        /// <summary>
        /// 获取页面的链接正则
        /// </summary>
        public string GetHref(string htmlCode)
        {
            const string reg = @"(h|H)(r|R)(e|E)(f|F) *= *('|"")?((\w|\\|\/|\.|:|-|_)+)[\S]*";
            return Regex.Matches(htmlCode, reg).Cast<Match>().Aggregate("", (current, m) => current + ((m.Value).ToLower().Replace("href=", "").Trim() + "|"));
        }

        #endregion

        #region 匹配页面的图片地址

        /// <summary>
        /// 匹配页面的图片地址
        /// </summary>
        /// <param name="htmlCode"></param>
        /// <param name="imgHttp">要补充的http://路径信息</param>
        public string GetImgSrc(string htmlCode, string imgHttp)
        {
            const string reg = @"<img.+?>";

            return Regex.Matches(htmlCode.ToLower(), reg).Cast<Match>().Aggregate("", (current, m) => current + (GetImg((m.Value).ToLower().Trim(), imgHttp) + "|"));
        }

        /// <summary>
        /// 匹配<img src="" />中的图片路径实际链接
        /// </summary>
        /// <param name="imgString"><img src="" />字符串</param>
        /// <param name="imgHttp"></param>
        public string GetImg(string imgString, string imgHttp)
        {
            const string reg = @"src=.+\.(bmp|jpg|gif|png|)";
            var matchVale = Regex.Matches(imgString.ToLower(), reg).Cast<Match>().Aggregate("", (current, m) => current + (m.Value).ToLower().Trim().Replace("src=", ""));
            if (matchVale.IndexOf(".net", StringComparison.Ordinal) != -1 || matchVale.IndexOf(".com", StringComparison.Ordinal) != -1 || matchVale.IndexOf(".org", StringComparison.Ordinal) != -1 ||
                matchVale.IndexOf(".cn", StringComparison.Ordinal) != -1 || matchVale.IndexOf(".cc", StringComparison.Ordinal) != -1 || matchVale.IndexOf(".info", StringComparison.Ordinal) != -1 ||
                matchVale.IndexOf(".biz", StringComparison.Ordinal) != -1 || matchVale.IndexOf(".tv", StringComparison.Ordinal) != -1)
                return (matchVale);
            return (imgHttp + matchVale);
        }

        #endregion

        #region 抓取远程页面内容

        /// <summary>
        /// 以GET方式抓取远程页面内容
        /// </summary>
        public static string Get_Http(string tUrl)
        {
            var strResult = string.Empty;
            try
            {
                var hwr = (HttpWebRequest) WebRequest.Create(tUrl);
                hwr.Timeout = 19600;
                var hwrs = (HttpWebResponse) hwr.GetResponse();
                var myStream = hwrs.GetResponseStream();
                if (myStream != null)
                {
                    var sr = new StreamReader(myStream, Encoding.Default);
                    var sb = new StringBuilder();
                    while (-1 != sr.Peek())
                    {
                        sb.Append(sr.ReadLine() + "\r\n");
                    }
                    strResult = sb.ToString();
                }
                hwrs.Close();
            }
            catch (Exception ee)
            {
                strResult = ee.Message;
            }
            return strResult;
        }

        /// <summary>
        /// 以POST方式抓取远程页面内容
        /// </summary>
        /// <param name="url"></param>
        /// <param name="postData">参数列表</param>
        /// <param name="encodeType"></param>
        public static string Post_Http(string url, string postData, string encodeType)
        {
            string strResult;
            try
            {
                var encoding = Encoding.GetEncoding(encodeType);
                var post = encoding.GetBytes(postData);
                var myRequest = (HttpWebRequest) WebRequest.Create(url);
                myRequest.Method = "POST";
                myRequest.ContentType = "application/x-www-form-urlencoded";
                myRequest.ContentLength = post.Length;
                var newStream = myRequest.GetRequestStream();
                newStream.Write(post, 0, post.Length); //设置POST
                newStream.Close();
                var myResponse = (HttpWebResponse) myRequest.GetResponse();
                var reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
                strResult = reader.ReadToEnd();
            }
            catch (Exception ex)
            {
                strResult = ex.Message;
            }
            return strResult;
        }

        #endregion

        #region 压缩HTML输出

        /// <summary>
        /// 压缩HTML输出
        /// </summary>
        public static string ZipHtml(string html)
        {
            html = Regex.Replace(html, @">\s+?<", "><"); //去除HTML中的空白字符
            html = Regex.Replace(html, @"\r\n\s*", "");
            html = Regex.Replace(html, @"<body([\s|\S]*?)>([\s|\S]*?)</body>", @"<body$1>$2</body>",
                RegexOptions.IgnoreCase);
            return html;
        }

        #endregion

        #region 过滤指定HTML标签

        /// <summary>
        /// 过滤指定HTML标签
        /// </summary>
        /// <param name="sTextStr">要过滤的字符</param>
        /// <param name="htmlStr">a img p div</param>
        public static string DelHtml(string sTextStr, string htmlStr)
        {
            var rStr = "";
            if (!string.IsNullOrEmpty(sTextStr))
            {
                rStr = Regex.Replace(sTextStr, "<" + htmlStr + "[^>]*>", "", RegexOptions.IgnoreCase);
                rStr = Regex.Replace(rStr, "</" + htmlStr + ">", "", RegexOptions.IgnoreCase);
            }
            return rStr;
        }

        #endregion

        #region 加载文件块

        /// <summary>
        /// 加载文件块
        /// </summary>
        public static string File(string path, System.Web.UI.Page p)
        {
            return @p.ResolveUrl(path);
        }

        #endregion

        #region 加载CSS样式文件

        /// <summary>
        /// 加载CSS样式文件
        /// </summary>
        public static string Css(string cssPath, System.Web.UI.Page p)
        {
            return @"<link href=""" + p.ResolveUrl(cssPath) + @""" rel=""stylesheet"" type=""text/css"" />" + "\r\n";
        }

        #endregion

        #region 加载JavaScript脚本文件

        /// <summary>
        /// 加载javascript脚本文件
        /// </summary>
        public static string Js(string jsPath, System.Web.UI.Page p)
        {
            return @"<script type=""text/javascript"" src=""" + p.ResolveUrl(jsPath) + @"""></script>" + "\r\n";
        }

        #endregion
    }
}
